<?php
class ControllerMessageOrder extends Controller {
	
	public function init(){
		$this->language->load('message/order');
		$this->load->model('message/order');
	}
	
	public function index() {
		$this->init();
		
		$this->type();
	}
	
	protected  function type(){
		$this->data['search'] = $this->url->link('message/order/query','',"SSL");
		$this->data['create'] = $this->url->link('message/order/create','',"SSL");
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/order_type.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/order_type.tpl';
		} else {
			$this->template = 'default/template/message/order_type.tpl';
		}

		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
	}
	
	public function create(){
		$this->init();
		
		$this->data['next'] = $this->url->link('message/order/next','','SSL');
		$this->data['before'] = $this->url->link('message/order','',"SSL");
		$this->data['bussnesss'] = $this->model_message_order->getAllBussnesss();
		
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/order_create_form.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/order_create_form.tpl';
		} else {
			$this->template = 'default/template/message/order_create_form.tpl';
		}
		
		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
	}
	
	public function query(){
		$this->init();
		if(isset($this->request->post['card_no'])){
			$card_no = $this->request->post['card_no'];
		}else{
			$card_no = '';
		}
		
		if(isset($this->request->post['bussness_type_id'])){
			$bussness_type_id = $this->request->post['bussness_type_id'];
		}else{
			$bussness_type_id = '';
		}
		
		$data = array(
			'card_no'=>$card_no,
			'bussness_type_id'=>$bussness_type_id
		);
		$this->data['search'] = $this->url->link('message/order/query','',"SSL");
		$this->data['bussnesss'] = $this->model_message_order->getAllBussnesss();
		$this->data['before'] = $this->url->link('message/order','',"SSL");
		$this->data['orderInfos'] = array();
		
		$orderBaseInfos = $this->model_message_order->getOrderBaseInfo($data);
		
		if($orderBaseInfos){
			foreach ($orderBaseInfos as $orderBaseInfo) {
				$this->data['orderInfos'][] =array(
					'card_no' =>$orderBaseInfo['card_no'],
					'type_name' =>$orderBaseInfo['type_name'],
					'summary' =>$orderBaseInfo['summary'],
					'href'=>$this->url->link('message/order/upload','card_no='.$orderBaseInfo['card_no']."&bussness_type_id=".$orderBaseInfo['bussness_type_id']."&order_id=".$orderBaseInfo['order_id'],'SSL'),
				);
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/order_query_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/order_query_list.tpl';
		} else {
			$this->template = 'default/template/message/order_query_list.tpl';
		}
		
		$this->data['card_no'] = $card_no;
		$this->data['bussness_type_id'] = $bussness_type_id;

		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
		
	}
	
	public function next(){
		$this->init();
		if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
			$orderId = $this->model_message_order->addOrderBaseInfo($this->request->post);
			$this->redirect(HTTPS_SERVER . 'index.php?route=message/order/upload&card_no='.$this->request->post['card_no']."&bussness_type_id=".(int)$this->request->post['bussness_type_id']."&order_id=".$orderId);
		}
	}
	
	public  function upload(){
		$this->init();
		$this->document->setTitle("预约");
			
		if(isset($this->request->get['sender_name'])){
			$sender_name=$this->request->get['sender_name'];
		}
		else{
			$sender_name = '';
		}
		
		if(isset($this->request->get['card_no'])){
			$this->data['card_no']=$this->request->get['card_no'];
		}
		else{
			$this->data['card_no'] = '';
		}
		
		if(isset($this->request->get['bussness_type_id'])){
			$this->data['bussness_type_id']=$this->request->get['bussness_type_id'];
			$bussnessInfo = $this->model_message_order->getBussness($this->data['bussness_type_id']);
		}
		else{
			$this->data['bussness_type_id'] = '';
		}
		
		
		if(isset($this->request->get['order_id'])){
			$this->data['order_id']=$this->request->get['order_id'];
			$orderInfo = $this->model_message_order->getOrderBaseInfoById($this->data['order_id']);
		}
		else{
			$this->data['order_id'] = '';
		}
		
		if($orderInfo){
			$this->data['summary'] = $orderInfo['summary'];
		}else{
			$this->data['summary'] ='';
		}
		
		if($bussnessInfo){
			$this->data['description'] =  html_entity_decode($bussnessInfo['description'], ENT_QUOTES, 'UTF-8');;
			$this->data['type_name'] = $bussnessInfo['type_name'];
		}else{
			$this->data['description'] ='';
			$this->data['type_name'] ='';
		}
		
		
		$this->data['bussnesss'] = $this->model_message_order->getAllBussnesss();
		
		$this->data['insert'] = $this->url->link('message/order/insert','','SSL');

		$this->data['uploadInfos'] = array();
		
		$results = $this->model_message_order->getOrderInfos($this->data['order_id']);
		if($results){
			foreach ($results as $result) {
				$this->data['uploadInfos'][]=array(
					'user_upload_info_id'=>$result['user_upload_info_id'],
					'name'=>$result['name'],
					'href'=>$this->url->link('message/order/delete','user_upload_info_id='.$result['user_upload_info_id']."&card_no=".$this->data['card_no']."&bussness_type_id=".(int)$this->data['bussness_type_id']."&order_id=".(int)$this->data['order_id'],'SSL'),
					'download'=>HTTP_SERVER.'download/'.$result['upload_path']
				);
			}
		}
		if(isset($bussnessInfo['code'])&&$bussnessInfo['code']=='NO_LINK'){
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/no_link.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/message/no_link.tpl';
			} else {
				$this->template = 'default/template/message/no_link.tpl';
			}
		}
		else{
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/order.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/message/order.tpl';
			} else {
				$this->template = 'default/template/message/order.tpl';
			}
		}
		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
	}

	public function insert() {
		$this->init();
		if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
			if (is_uploaded_file($this->request->files['fileField']['tmp_name'])) {
				$filename = $this->request->files['fileField']['name'] . '.' . md5(rand());

				move_uploaded_file($this->request->files['fileField']['tmp_name'], DIR_DOWNLOAD . $filename);

				if (file_exists(DIR_DOWNLOAD . $filename)) {
					
					$data = array(
						'upload_path'=>$filename,
						'file_name'=>$this->request->files['fileField']['name'],
						'card_no'=>$this->request->post['card_no'],
						'name'=>$this->request->post['name'],
						'bussness_type_id'=>$this->request->post['bussness_type_id'],
						'order_id'=>$this->request->post['order_id'],
					);
					$this->model_message_order->addOrderInfo("22232332",$data);
				}
				$this->redirect(HTTPS_SERVER . 'index.php?route=message/order/upload&card_no='.$this->request->post['card_no']."&bussness_type_id=".$this->request->post['bussness_type_id']."&order_id=".(int)$this->request->post['order_id']);
			}
		}
		$this->index();
	}
	
	public function delete(){
		$this->init();
		
		if(isset($this->request->get['user_upload_info_id'])){
			$uploadInfo= $this->model_message_order->getOrderInfo($this->request->get['user_upload_info_id']);
			if($uploadInfo){
				$filePath = $uploadInfo['upload_path'];
				if (file_exists(DIR_DOWNLOAD.$filePath)) {
					$this->model_message_order->deleteOrderInfo($this->request->get['user_upload_info_id']);
					unlink(DIR_DOWNLOAD.$filePath);
				}
				$this->redirect(HTTPS_SERVER . 'index.php?route=message/order/upload&card_no='.$this->request->get['card_no']."&bussness_type_id=".$this->request->get['bussness_type_id']."&order_id=".(int)$uploadInfo['order_id']);
			}
		}
		
		$this->index();
	}
	
	public function check(){
		$this->load->library('json');
		$this->load->model('message/order');
		if(isset($this->request->get['filter_card_no'])){
			$filter_card_no = $this->request->get['filter_card_no'];
		}else{
			$filter_card_no = null;
		}
		if(isset($this->request->get['bussness_type_id'])){
			$bussness_type_id = $this->request->get['bussness_type_id'];
		}else{
			$bussness_type_id = null;
		}
		
		$data = array(
			'card_no'=>$filter_card_no,
			'filter_date_added'=>date('y-m-d',time()),
			'bussness_type_id'=>$bussness_type_id
		);
		$json= array();
		$orderBaseInfo = $this->model_message_order->getOrderBaseInfo($data);
		if(!$orderBaseInfo){
			$json['result'] = "success";
		}else{
			$json['result'] = "faile";
		}
		
		$this->response->setOutput(Json::encode($json));
	}
	
	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$this->redirect(HTTPS_SERVER . 'index.php?route=message/order');
	}
}
?>